from manim import *



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果
        self.camera.frame.save_state()
        self.play(FadeIn(dot1),FadeIn(dot2),FadeIn(dot3))
         #中心主题+框框标记
        text1 = Tex(r"\text{换元法}",color=PINK
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,5,0])
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=13,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-40, 5, 0])

        self.play(Create(rectangle),Create(text1))

        #箭头引出
        arrow = Arrow(start=[-33,7,0],end=[-25,15,0], color=RED
                       , stroke_width=20, tip_length=1)
        #箭头引出
        arrow1 = Arrow(start=[-33,3,0],end=[-25,0,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow),Create(arrow1))

        #文本
        text2 = Tex(r"\text{整体换元}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-18,15,0])
         #箭头引出
        arrow2 = Arrow(start=[-12,15,0],end=[-5,15,0], color=RED
                       , stroke_width=20, tip_length=1)
        text6 = Tex(r"\text{特征:}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-7,15,0])
        arrow3 = Arrow(start=[-12,0,0],end=[-5,0,0], color=RED
                       , stroke_width=20, tip_length=1)
        text7 = Tex(r"\text{特征:}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-7,0,0])
        text4 = Tex(r"$\int \dfrac{\ln ^{2}x}{x}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,15,0])
        text3 = Tex(r"\text{三角换元}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-18,0,0])
        text5 = Tex(r"$\int \dfrac{x+1}{x\left( 1+xe^{x}\right) }dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,0,0])
        
        text8 = Tex(r"$\sqrt{ax+b},\sqrt{\dfrac{ax+b}{cx+d}}$\text{  例：}$\int \dfrac{1}{\sqrt{x}\left( 1+x\right) }dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,20,0])
        text9 = Tex(r"$\sqrt{ae^{x}+b},\sqrt{\dfrac{ae^{x}+b}{ce^{x}+d}}$\text{  例：}$\int \dfrac{1}{\sqrt{e^{x}-1}}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,10,0])
        
        text10 = Tex(r"$\sqrt{a^{2}-x^{2}}$\text{  例：}$\dfrac{1}{x\sqrt{1-x^{2}}}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,0,0])
        text11 = Tex(r"$\sqrt{a^{2}+x^{2}}$\text{  例：}$\int \dfrac{1}{\sqrt{x^{2}+a^{2}}}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,-10,0])
        text12 = Tex(r"$\sqrt{x^{2}-a^{2}}$\text{  例：}$\int \dfrac{1}{\sqrt{x^{2}-a^{2}}}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,-20,0])
        

        
        
        self.play(Create(text2),Create(text3))
        #self.play(Create(arrow2),Create(arrow3))
        self.play(Create(text6),Create(text7))
        self.play(Create(text8),Create(text9))
        self.play(Create(text10),Create(text11),Create(text12))
        

        #框框标记
        
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=17,height=8
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle.move_to([33,20,0])
        self.play(Create(rectangle))

        text13 = Tex(r"$\int \dfrac{1}{\sqrt{x}\left( 1+x\right) }dx$",color=RED_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-7,0])
        self.play(Create(text13))
        self.wait(0.1)
        text14 = Tex(r"$=\int \dfrac{1}{t\left( 1+t^{2}\right) }\cdot dt^{2}$",color=YELLOW_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-14,0])
        self.play(Create(text14))
        self.wait(0.1)
        text15 = Tex(r"$(t=\sqrt{x})$",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text14,RIGHT,buff=2)
        self.play(Create(text15))
        self.wait(0.1)
        text16 = Tex(r"$=\int \dfrac{2}{1+t^{2}}dt$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        self.play(Create(text16))
        self.wait(0.1)
        text17 = Tex(r"$=2\arctan t+c$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        text18 = Tex(r"$=2\arctan \sqrt{x}+c$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        self.play(
            ReplacementTransform(text16,text17)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text17,text18)
        )
        self.wait(0.5)

        #清除部分元素+框框移动
        self.play(
            FadeOut(text18),FadeOut(text15),FadeOut(text14),FadeOut(text13)
            ,rectangle.animate().move_to([34,10,0]).stretch(0.9,dim=0)

        )

        text13 = Tex(r"$\int \dfrac{1}{\sqrt{e^{x}-1}}dx$",color=RED_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-7,0])
        self.play(Create(text13))
        self.wait(0.1)
        text14 = Tex(r"$=\int \dfrac{1}{t}dln\left( t^{2}+1\right) $",color=YELLOW_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-14,0])
        self.play(Create(text14))
        self.wait(0.1)
        text15 = Tex(r"$(t=\sqrt{e^{x}-1})$",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text14,RIGHT,buff=2)
        self.play(Create(text15))
        self.wait(0.1)
        text16 = Tex(r"$=\int \dfrac{2}{1+t^{2}}dt$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        self.play(Create(text16))
        self.wait(0.1)
        text17 = Tex(r"$=2\arctan t+c$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        text18 = Tex(r"$=2\arctan \sqrt{e^{x}-1}+c$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        self.play(
            ReplacementTransform(text16,text17)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text17,text18)
        )
        self.wait(0.5) 





        #清除部分元素+框框移动
        self.play(
            FadeOut(text18),FadeOut(text15),FadeOut(text14),FadeOut(text13)
            ,rectangle.animate().move_to([28,0,0]).stretch(1,dim=0)

        )

        text13 = Tex(r"$\int \dfrac{1}{x\sqrt{1-x^{2}}}dx$",color=RED_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-7,0])
        self.play(Create(text13))
        self.wait(0.1)
        text14 = Tex(r"$=\int \dfrac{1}{\sin t\cos t}d\sin t$",color=YELLOW_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-14,0])
        self.play(Create(text14))
        self.wait(0.1)
        text15 = Tex(r"$(x=\sin t)$",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text14,RIGHT,buff=2)
        self.play(Create(text15))
        self.wait(0.1)
        text16 = Tex(r"$=\int \dfrac{1}{\sin t}dt$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        self.play(Create(text16))
        self.wait(0.1)
        text17 = Tex(r"$=\int \dfrac{\sin t}{\sin ^{2}t}dt$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        text18 = Tex(r"$=-\int \dfrac{1}{1-\cos ^{2}t}d\cos t$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        text19 = Tex(r"$=-\dfrac{1}{2}\int \left( \dfrac{1}{1-\cos t}+\dfrac{1}{1+\cos t}\right) d\cos t$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        text20 = Tex(r"$=-\dfrac{1}{2}\ln \left| \dfrac{1+\cos t}{1-\cos t}\right| +c$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        text21 = Tex(r"$=\ln \sqrt{\dfrac{1-\cos t}{1+\cos t}}+c$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        text22 = Tex(r"$=\ln \dfrac{1-\sqrt{1-x^{2}}}{x}+c$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        self.play(
            ReplacementTransform(text16,text17)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text17,text18)
        )
        self.wait(0.5) 
        self.play(
            ReplacementTransform(text18,text19)
        )
        self.wait(0.5) 
        self.play(
            ReplacementTransform(text19,text20)
        )
        self.wait(0.5) 
        self.play(
            ReplacementTransform(text20,text21)
        )
        self.wait(0.5) 
        self.play(
            ReplacementTransform(text21,text22)
        )
        self.wait(0.5) 




        #清除部分元素+框框移动
        self.play(
            FadeOut(text22),FadeOut(text15),FadeOut(text14),FadeOut(text13)
            ,rectangle.animate().move_to([28.5,-10,0]).stretch(1,dim=0)

        )

        text13 = Tex(r"$\int \dfrac{1}{\sqrt{x^{2}+a^{2}}}dx$",color=RED_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-7,0])
        self.play(Create(text13))
        self.wait(0.1)
        text14 = Tex(r"$=\int \dfrac{1}{a\sec t}da\cdot \tan t$",color=YELLOW_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-14,0])
        self.play(Create(text14))
        self.wait(0.1)
        text15 = Tex(r"$(x=a\cdot \tan t)$",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text14,RIGHT,buff=2)
        self.play(Create(text15))
        self.wait(0.1)
        text16 = Tex(r"$=\int \sec tdt$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        self.play(Create(text16))
        self.wait(0.1)
        text17 = Tex(r"$=\int \dfrac{1}{\cos t}dt$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        text18 = Tex(r"$=\ln \sqrt{\dfrac{1+\sin t}{1-\sin t}}+c$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        text19 = Tex(r"$=ln\left( x+\sqrt{x^{2}+a^{2}}\right) +c$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        
        self.play(
            ReplacementTransform(text16,text17)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text17,text18)
        )
        self.wait(0.5) 
        self.play(
            ReplacementTransform(text18,text19)
        )
        self.wait(0.5) 

        #清除部分元素+框框移动
        self.play(
            FadeOut(text19),FadeOut(text15),FadeOut(text14),FadeOut(text13)
            ,rectangle.animate().move_to([28.5,-20,0]).stretch(1,dim=0)

        )

        text13 = Tex(r"$\int \dfrac{1}{\sqrt{x^{2}-a^{2}}}dx$",color=RED_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-7,0])
        self.play(Create(text13))
        self.wait(0.1)
        text14 = Tex(r"$=\int \dfrac{1}{a\tan t}da\sec t$",color=YELLOW_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-14,0])
        self.play(Create(text14))
        self.wait(0.1)
        text15 = Tex(r"$(x=a\sec t)$",color=GREEN_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text14,RIGHT,buff=2)
        self.play(Create(text15))
        self.wait(0.1)
        text16 = Tex(r"$=\int \sec tdt$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        self.play(Create(text16))
        self.wait(0.1)
        text17 = Tex(r"$=\int \dfrac{1}{\cos t}dt$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        text18 = Tex(r"$=\ln \sqrt{\dfrac{1+\sin t}{1-\sin t}}+c$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        text19 = Tex(r"$=ln\left( x+\sqrt{x^{2}-a^{2}}\right) +c$",color=BLUE_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-21,0])
        
        self.play(
            ReplacementTransform(text16,text17)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text17,text18)
        )
        self.wait(0.5) 
        self.play(
            ReplacementTransform(text18,text19)
        )
        self.wait(0.5) 


        






        self.wait()